<?php


namespace App\Http\Controllers\Api;
use App\Http\Controllers\BaseController;
use App\Models\Cookbook;
use App\Models\Star;
use App\Models\UserViewHistory;
use App\Services\OrderService;
use App\Services\UserViewHistoryService;
use Dingo\Api\Http\Request;

class CookbookController extends BaseController
{

    public $model = Cookbook::class;
    /**
     * 列表
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request) {
        $title = $request->input('title');
        $category_id = $request->input('category_id');
        $order_by_key = $request->input('order_by_key', 'sort');
        $order_by_value = $request->input('order_by_value', 'desc');
        //观看历史
        $is_my_history = $request->input('is_my_history');
        $is_relate_rec = $request->input('is_relate_rec', 0);


        $is_on = 1; //上架
        $is_vip = $request->input('is_vip');
        $fields = ['id', 'title', 'short_title', 'is_free', 'is_vip', 'price', 'short_video_url', 'long_time', 'difficulty', 'category_id'];

        $list = Cookbook::select($fields)->title($title)
            ->category($category_id)
            ->with('category')
            ->when($is_my_history, function ($q) {
                $q->whereHas('view_history', function ($qr) {
                    $user_id = $this->getUserId();
                    $qr->where('user_id', $user_id);
                });
            })
            ->isOn($is_on)->when($is_vip, function ($q)use($is_vip) {
                $q->where('is_vip', '=', $is_vip);
            });

        if ($is_relate_rec) {
            $list = $list->inRandomOrder()->paginate(\request('per_page', 2));
        } else {
            $list = $list->orderBy($order_by_key, $order_by_value)->orderBy("id", "desc")
                ->paginate(\request('per_page', 20));
        }
        return $this->paginate($list);
    }

    public function info(Request $request) {
        $id = \request("id");
        if (!$id) {
            return $this->failed("参数ID不存在");
        }
        $user = $this->getUser();
        $cook = Cookbook::userStar($user->id)->with('category')->find($id);
        if ($cook->is_on == 0 || $cook->audit_status != 1)
            return $this->failed("商品已下架");

        if ($cook->is_vip && !$user->is_vip) {
            return $this->failed("无权查看，请先开通会员");
        }

        //判断是否购买
        if (!OrderService::is_buy($user->id, $id)) {
            $field = ['video_url', 'long_time', 'difficulty', 'calorie', 'proteide', 'carbohydrate', 'fat', 'list_food', 'step'];
            $cook->setHidden($field);
        } else {
            UserViewHistoryService::store($user->id, $id);
        }

        return $this->success($cook);
    }

    public function viewHistory(Request $request) {
        $title = $request->input('title');
        $user = $this->getUser();
        $list = UserViewHistory::where('user_id', $user->id)->when($title, function ($q) use($title){
            $q->whereHas('cookbook', function ($query) use($title){
               $query->where('title', 'like', "%$title%");
            });
        })->withCertain('cookbook', ['title','short_video_url', 'short_title'])
            ->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }


    public function collect(Request $request) {
        $this->validate($request, [
            'user_id' => 'required'
        ]);
        $user_id = \request('user_id');
        $list = Star::where('user_id', $user_id)->where('type', 1)
            ->withCertain('cookbook', ['title','short_video_url', 'short_title'])
            ->orderBy("id", "desc")
            ->paginate(\request('per_page', 20));
        return $this->paginate($list);
    }


}

